package lovo01.lesson06;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 发牌器
 */

public class DouDiZhu{

    public ArrayList<PuKe> puKes = new ArrayList<>();
//
//    public DouDiZhu(){
//        //设置布局为空
//        this.setLayout(null);
//        //设置按钮
//        JButton button = new JButton("开始");
//        button.setBounds(550,350,100,100);
//        //将组件添加到容器
//        this.add(button);
//        //窗口名称
//        this.setTitle("神仙斗地主");
//        //设置窗口大小
//        this.setSize(1200,800);
//        //窗口位置居中
//        this.setLocationRelativeTo(null);
//        //显示窗口
//        this.setVisible(true);
//    }
    /**
     * 初始化54章扑克牌
     */
    public void init(){
        String[] points = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
        String[] types = "♠,♥,♣,♦".split(",");
        for(int i = 0;i < points.length;i++){
            for (int j = 0;j < types.length;j++){
                puKes.add(new PuKe(points[i],types[j],i));
            }
        }
        puKes.add(new PuKe("大王","",14));
        puKes.add(new PuKe("小王","",13));

    }

    /**
     * 洗牌
     */
    public void shuffle(){
        Collections.shuffle(puKes);
        System.out.println(puKes);
    }

    /**
     * 发牌
     */
    public void sendCard(){
        ArrayList<PuKe> player1 = new ArrayList<>(puKes.subList(0,17));
        ArrayList<PuKe> player2 = new ArrayList<>(puKes.subList(17,34));
        ArrayList<PuKe> player3 = new ArrayList<>(puKes.subList(34,51));
        ArrayList<PuKe> cardHand = new ArrayList<>(puKes.subList(51,54));

        sort(player1);
        sort(player2);
        sort(player3);

        System.out.println("玩家一：" + player1);
        System.out.println("玩家二：" + player2);
        System.out.println("玩家三：" + player3);
        System.out.println("玩家四：" + cardHand);
    }

    private void sort(ArrayList list){
        Collections.sort(list, new Comparator<PuKe>() {
            @Override
            public int compare(PuKe o1, PuKe o2) {
                return (o1.getPointValue() - o2.getPointValue());
            }
        });
    }

    public static void main(String[] args) {
        DouDiZhu douDiZhu = new DouDiZhu();
        douDiZhu.init();
        douDiZhu.shuffle();
        douDiZhu.sendCard();

        new PukeFrame(douDiZhu);
    }

}
